package de.hwrberlin.remo.algorithm.util;

import java.util.List;

import de.hwrberlin.remo.algorithm.steps.AlgorithmStep;

/**
 * The AlgorithmDebugger is used to walk through the 
 * steps of an algorithm. 
 * 
 * @author Mario Sänger (IT 09)
 * @version 1.0
 *
 */
public class AlgorithmDebugger {

	private List<AlgorithmStep> steps;
	private int currentStep;
	
	///////////////////////
	/* Constructor       */
	
	public AlgorithmDebugger(List<AlgorithmStep> steps) {
		this.steps       = steps;
		this.currentStep = -1;
	}
	
	///////////////////////
	/* Control methods   */
	
	public void next() {		
		if(!isEnd()) {
			currentStep++;
			steps.get(currentStep).perform();
		}
	}
	
	public void back() {
		if(!isStart()) {
			steps.get(currentStep).revert();
			currentStep--;
		}
	}
	
	public boolean isEnd() {
		if(steps != null) {
			return (steps.size() - 1) == currentStep;
		}
		
		return true;
	}
	
	public boolean isStart() {
		return currentStep == -1; 
	}
	
	public AlgorithmStep getCurrentStep() {
		if(steps != null) {
			return steps.get(currentStep);
		}
		
		return null;
	}
	
	public void gotoLast() {
		while(!isEnd()) {
			next();
		}
	}
	
	public void gotoFirst() {
		while(!isStart()) {
			back();
		}
	}
	
	public int getStepCount() {
		if(steps != null) {
			return steps.size();
		}
		
		return 0;
	}
	
	public int getCurrentStepNumber() {
		return currentStep + 1; 
	}
}